#!/bin/bash
#description: install php-memcached for centos7.3
SOFTDIR=/usr/local/src
LIBMEMCACHED_VERSION=1.0.18
LIBMEMCACHED_DIR=/usr/local/libmemcached
PHPDIR=/usr/local/php5
PHP_MEMCACHED_VERSION=2.2.0
#Install dependencies
yum -y install autoconf automake make
#install libmemcached
libmemcached(){
	cd $SOFTDIR && [ ! -f libmemcached-${LIBMEMCACHED_VERSION}.tar.gz ] && wget http://soft.51yuki.cn/libmemcached-${LIBMEMCACHED_VERSION}.tar.gz
	[ ! -d libmemcached-${LIBMEMCACHED_VERSION} ] && tar zxf libmemcached-${LIBMEMCACHED_VERSION}.tar.gz 
	cd libmemcached-${LIBMEMCACHED_VERSION}
	./configure --prefix=${LIBMEMCACHED_DIR} && make && make install
	if [ $? -ne 0 ];then
		echo "install libmemcached fail"
		exit 2
	fi
}
#install php-memcached

php_memcached(){
	cd $SOFTDIR && [ ! -f memcached-${PHP_MEMCACHED_VERSION}.tgz ] && wget http://soft.51yuki.cn/memcached-${PHP_MEMCACHED_VERSION}.tgz
	[ ! -d memcached-${PHP_MEMCACHED_VERSION} ] && tar xf memcached-${PHP_MEMCACHED_VERSION}.tgz 
	cd memcached-${PHP_MEMCACHED_VERSION}
	$PHPDIR/bin/phpize 
	./configure --with-php-config=${PHPDIR}/bin/php-config --with-libmemcached-dir=${LIBMEMCACHED_DIR}  --disable-memcached-sasl 
	make && make install
}
#copy memcached.so 
copy_memcached(){
	grep memcached.so ${PHPDIR}/etc/php.ini 
	if [ $? -ne 0 ];then
		echo "extension=memcached.so" >> ${PHPDIR}/etc/php.ini
	else
		echo "memcached.so is existing"
	fi
}
main(){
	libmemcached
	php_memcached
	copy_memcached
}
main

